package org.space.galaxyCabinet.clients;

import com.galaxy.common.mq.chlients.MQClient;
import com.galaxy.common.mq.config.RabbitConfig;
import com.galaxy.common.mq.config.RabbitTtlQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Slf4j
@Component
public class RabbitMQOrderClient implements MQClient {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public boolean sendDelayMessage(String message, long delay) {
        try {
            rabbitTemplate.convertAndSend(
                RabbitTtlQueueConfig.DELAY_EXCHANGE_NAME,
                RabbitTtlQueueConfig.DELAY_ROUTING_KEY,
                message, message1 -> {
                    message1.getMessageProperties().setDelayLong(delay);
                    return message1;
                });
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean sendMessage(String message) {
        String correlationId = UUID.randomUUID().toString();
        CorrelationData correlationData = new CorrelationData(correlationId);

        log.info("发送消息，ID: {}, 内容: {}", correlationId, message);

        rabbitTemplate.convertAndSend(
            RabbitConfig.QUEUE_GOODS_OUT,
            RabbitConfig.GOODS_OUT_KEY,
            message,
            correlationData
        );
        return true;
    }

    @Override
    public boolean sendTimoutMessage(String message, long delay) {
        return false;
    }
}

